<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="1000" destroy-on-close @closed="$emit('closed')" align-center>
        <el-container>
            <el-container>
                <el-header>
                    <div class="left-panel">
                        <el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
                    </div>
                    <div class="right-panel">
                        <div class="right-panel-search">
                            <el-input v-model="search.recommend_name" placeholder="推介人姓名" clearable @keyup.enter="upsearch"></el-input>
                            <el-input v-model="search.recommend_phone" placeholder="联系方式" clearable @keyup.enter="upsearch"></el-input>
                            <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                        </div>
                    </div>
                </el-header>
                <el-main class="nopadding">
                    <scTable ref="table" row-key="id" :apiObj="apiObj" highlight-current-row @row-click="selectionChange" stripe remoteSort remoteFilter border height="550">
                    <el-table-column width="1"></el-table-column>
					<el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
					<el-table-column label="推介人姓名" prop="recommend_name"></el-table-column>
                    <el-table-column label="联系方式" prop="recommend_phone"></el-table-column>
                    <el-table-column label="创建时间" prop="create_time"></el-table-column>
                    </scTable>
                </el-main>
            </el-container>
        </el-container>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
		</template>
        <save-add v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-add>
	</el-dialog>
</template>

<script>
import saveAdd from './save.vue'
export default {
  components: { saveAdd },
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '选择推介人',
				edit: '编辑产品',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			apiObj: this.$API.product.referrer.list,
			selection: [],
			search: {
                recommend_name: null,
                recommend_phone: null,
			},
            dialog: {
				save: false,
			},
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单注入数据
		setData(data) {
			//可以和上面一样单个注入，也可以像下面一样直接合并进去
            this.form.id = data.id
		},
        //搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
        // 控制单选
        selectAll() {
			this.$message.error("只能单选！")
			this.$refs.table.clearSelection()
		},
        selectionChange(selection) {
			// if(selection.length > 1){
			// 	const del_row = selection.shift()
			// 	this.$refs.table.toggleRowSelection(del_row,false) 
			// }
			// this.selection = selection;
            this.$emit('success', selection)
            this.visible = false;
		},
        //客户新增
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add')
			})
		},
        // 更新客户新增
        handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			}
		},
	}
}
</script>

<style></style>
